/*
 * 
 *  jiangcheng
 *  
 */

package com.uhaou.web.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

import cn.org.rapid_framework.beanutils.BeanUtils;
import cn.org.rapid_framework.web.scope.Flash;
 
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;

import java.util.*;
import org.apache.commons.lang.StringUtils;

import com.uhaou.common.base.*;
import com.uhaou.common.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.uhaou.web.model.*;
import com.uhaou.web.dao.*;
import com.uhaou.web.service.*;
import com.uhaou.web.vo.query.*;

/**
 * @author  jiangcheng
 * @version 1.0
 * @since 1.0
 */


public class WebFriendAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String QUERY_JSP = "/web/WebFriend/query.jsp";
	protected static final String LIST_JSP= "/web/WebFriend/list.jsp";
	protected static final String CREATE_JSP = "/web/WebFriend/create.jsp";
	protected static final String EDIT_JSP = "/web/WebFriend/edit.jsp";
	protected static final String SHOW_JSP = "/web/WebFriend/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/web/WebFriend/list.do";
	
	private IWebFriendManager webFriendManager;
	
	private WebFriend webFriend;
	java.lang.String id = null;
	private String[] items;
	private String param;
	
	public void setParam(String param){
		this.param = param;
	}

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			webFriend = new WebFriend();
		} else {
			webFriend = (WebFriend)webFriendManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setWebFriendManager(IWebFriendManager manager) {
		this.webFriendManager = manager;
	}	
	
	public Object getModel() {
		return webFriend;
	}
	
	public void setId(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/** 执行搜索 */
	public String list() {
		return LIST_JSP;
	}
	
	public String listAjax(){
		WebFriendQuery query = newQuery(WebFriendQuery.class,DEFAULT_SORT_COLUMNS);
		Page page = webFriendManager.findPage(query);
		Map map = new HashMap();
		map.put("total", page.getTotalCount());
		map.put("rows", page.getResult());
		return writeAjaxResponse(Json.toJson(map));
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	
	/** 保存新增对象 */
	public String save() {
		webFriendManager.save(webFriend);
		return LIST_ACTION;
	}
	
	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}
	
	/**保存更新对象*/
	public String update() {
		webFriendManager.update(this.webFriend);
		return LIST_ACTION;
	}
	
	/**删除对象*/
	public String delete() {
		if(items!=null)
			for(String ids:items){
				String[] idArray = ids.split(",");
				for(String id:idArray)
				webFriendManager.removeById(id);
			}
		return LIST_ACTION;
	}

}
